from scipy.stats import chi2_contingency, pearsonr
import pandas as pd
import matplotlib.pyplot as plt
from utils.Feature_selection import feature_engineering
# 解决中文显示问题（核心修复代码）
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
import joblib
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score, roc_auc_score
import seaborn as sns


es = joblib.load(r'../data/123.pkl')

x, y,x_list=feature_engineering(r'../new_project/data/train.csv')


x=pd.get_dummies(x)
y_pro = es.predict(x)


# 混淆矩阵
cm = confusion_matrix(y, y_pro)
plt.figure(figsize=(5, 4))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.xlabel('预测类别')
plt.ylabel('真实类别')
plt.title('混淆矩阵')
plt.tight_layout()
plt.show()

# 模型属性自适应打印，兼容GridSearchCV和基础模型
print('模型类型:', type(es))
if hasattr(es, 'best_params_'):
    print('最优参数:', es.best_params_)
    print('最优交叉验证得分:', es.best_score_)
    print('最优子模型:', es.best_estimator_)
    print('评分函数:', es.scorer_)
    print('交叉验证折数:', es.n_splits_)
    # print('交叉验证详细结果:', es.cv_results_)
else:
    print('模型参数:', es.get_params())
    if hasattr(es, 'coef_'):
        print('模型系数:', es.coef_)
    if hasattr(es, 'intercept_'):
        print('截距:', es.intercept_)



print("Precision:", precision_score(y, y_pro))  # 修改为使用y_pre
print("Recall:", recall_score(y, y_pro))
print("F1:", f1_score(y, y_pro))
print("ROC AUC:", roc_auc_score(y, y_pro))
# print("最佳参数:", new_es.best_params_)
# print("最佳得分:", new_es.best_score_)